package com.ebt.m.proposal_v2.record;

import java.io.File;
import java.io.IOException;

/**
 * MP3格式录音API
 *
 * @author archer.qi
 *         Created on 2017/6/14.
 */
public class MP3RecorderApi {
    private com.czt.mp3recorder.MP3Recorder mRecorder;

    /**
     * 生成录音存储文件
     *
     * @param dir
     * @param fileName
     * @return
     */
    public File getRecorderTarget(String dir, String fileName) {
        File dirFile = new File(dir);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }

        File targetFile = new File(dir, fileName);
        if (targetFile.exists()) {
            targetFile.deleteOnExit();
        }
        try {
            targetFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return targetFile;
    }

    /**
     * 开始MP3录音
     *
     * @param targetDir
     * @param fileName
     * @throws IOException
     */
    public void startMP3Recorder(String targetDir, String fileName) throws IOException, IllegalStateException {
        File recorderTargetFile = getRecorderTarget(targetDir, fileName);
        mRecorder = new com.czt.mp3recorder.MP3Recorder(recorderTargetFile);
        mRecorder.start();
    }

    /**
     * 结束录音
     */
    public void stopMP3Recorder() {
        if (mRecorder != null && mRecorder.isRecording()) {
            mRecorder.stop();
            mRecorder = null;
        }
    }

    /**
     * 取得最大音量
     *
     * @return
     * @throws NullPointerException
     */
    public int getMaxVolume() {
        if (mRecorder == null) {
            throw new NullPointerException();
        }
        return mRecorder.getMaxVolume();
    }

    /**
     * 取得相对音量
     *
     * @return
     */
    public int getVolume() {
        if (mRecorder == null) {
            throw new NullPointerException();
        }
        return mRecorder.getVolume();
    }

    /**
     * 取得真实音量
     *
     * @return
     */
    public int getRealVolume() {
        if (mRecorder == null) {
            throw new NullPointerException();
        }
        return mRecorder.getRealVolume();
    }

    /**
     * 是否正在录音
     *
     * @return
     */
    public boolean isRecording() {
        if (mRecorder == null) {
            throw new NullPointerException();
        }
        return mRecorder.isRecording();
    }

}
